package mutsumimod.cards.custom;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.GainBlockAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import mutsumimod.actions.TrueDamageCallbackAction;
import mutsumimod.cards.BaseCard;
import mutsumimod.character.Mutsumi;
import mutsumimod.util.CardStats;

public class DoubleHurt extends BaseCard {
    public static final String ID = makeID(DoubleHurt.class.getSimpleName());
    public static final CardStats info = new CardStats(
            Mutsumi.Meta.CARD_COLOR,
            CardType.ATTACK,
            CardRarity.COMMON,
            CardTarget.ENEMY,
            1
    );

    public DoubleHurt() {
        super(ID, info);
        setAttr(5,0,2);
        setAttrUpg(2,0,0);
    }

    private int lastDamagetaken[]=new int[]{-1,-1};

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        addToBot(new TrueDamageCallbackAction(m, new DamageInfo(AbstractDungeon.player
                , damage, DamageInfo.DamageType.NORMAL), AbstractGameAction.AttackEffect.SLASH_VERTICAL,
                (dmg) -> {
                    lastDamagetaken[0] = dmg;
                    checkAndApplyBlock(m);
                }
        ));

        addToBot(new TrueDamageCallbackAction(m, new DamageInfo(AbstractDungeon.player
                , damage, DamageInfo.DamageType.NORMAL), AbstractGameAction.AttackEffect.SLASH_VERTICAL,
                (dmg) -> {
                    lastDamagetaken[1] = dmg;
                    checkAndApplyBlock(m);
                }
        ));
    }

    private void checkAndApplyBlock(AbstractMonster m) {
        if (lastDamagetaken[0] != -1 && lastDamagetaken[1] != -1) {
            int difference = Math.abs(lastDamagetaken[0] - lastDamagetaken[1]);
            if(difference!=0){
                addToTop(new GainBlockAction(AbstractDungeon.player, difference));
            }
            lastDamagetaken = new int[]{-1, -1};
        }
    }

    @Override
    public AbstractCard makeCopy() { //Optional
        return new DoubleHurt();
    }
}
